using System;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;

namespace AntEater.Integration
{
    /// <summary>
    /// Wrapper class for listening to the solution events
    /// </summary>
    internal class SolutionListener : IVsSolutionEvents
    {
        /// <summary>
        /// A solution was opened by Visual Studio
        /// </summary>
        public event EventHandler<EventArgs> SolutionOpened;

        /// <summary>
        /// A solution was closed by Visual Studio
        /// </summary>
        public event EventHandler<EventArgs> SolutionClosed;

        /// <summary>
        /// A project was opened by Visual Studio
        /// </summary>
        public event EventHandler<EventArgs> ProjectOpened;

        /// <summary>
        /// A project was closed by Visual Studio
        /// </summary>
        public event EventHandler<EventArgs> ProjectClosed;

        /// <summary>
        /// Called when [after open solution].
        /// </summary>
        /// <param name="pUnkReserved">The p unk reserved.</param>
        /// <param name="fNewSolution">The f new solution.</param>
        /// <returns></returns>
        int IVsSolutionEvents.OnAfterOpenSolution(object pUnkReserved, int fNewSolution) {
            if (SolutionOpened != null) {
                SolutionOpened(this, new EventArgs());
            }
            return VSConstants.S_OK;
        }

        /// <summary>
        /// Called when [after close solution].
        /// </summary>
        /// <param name="pUnkReserved">The p unk reserved.</param>
        /// <returns></returns>
        int IVsSolutionEvents.OnAfterCloseSolution(object pUnkReserved) {
            if (SolutionClosed != null) {
                SolutionClosed(this, new EventArgs());
            }
            return VSConstants.S_OK;
        }

        /// <summary>
        /// Called when [after open project].
        /// </summary>
        /// <param name="pHierarchy">The p hierarchy.</param>
        /// <param name="fAdded">The f added.</param>
        /// <returns></returns>
        int IVsSolutionEvents.OnAfterOpenProject(IVsHierarchy pHierarchy, int fAdded) {
            if (ProjectOpened != null) {
                ProjectOpened(this, new EventArgs());
            }
            return VSConstants.S_OK;
        }

        /// <summary>
        /// Called when [before close project].
        /// </summary>
        /// <param name="pHierarchy">The p hierarchy.</param>
        /// <param name="fRemoved">The f removed.</param>
        /// <returns></returns>
        int IVsSolutionEvents.OnBeforeCloseProject(IVsHierarchy pHierarchy, int fRemoved) {
            if (ProjectClosed != null) {
                ProjectClosed(this, new EventArgs());
            }
            return VSConstants.S_OK;
        }


        /// <summary>
        /// Called when [after load project].
        /// </summary>
        /// <param name="pStubHierarchy">The p stub hierarchy.</param>
        /// <param name="pRealHierarchy">The p real hierarchy.</param>
        /// <returns></returns>
        int IVsSolutionEvents.OnAfterLoadProject(IVsHierarchy pStubHierarchy, IVsHierarchy pRealHierarchy) {
            if (ProjectOpened != null) {
                ProjectOpened(this, new EventArgs());
            }
            return VSConstants.S_OK;
        }

        /// <summary>
        /// Called when [before unload project].
        /// </summary>
        /// <param name="pRealHierarchy">The p real hierarchy.</param>
        /// <param name="pStubHierarchy">The p stub hierarchy.</param>
        /// <returns></returns>
        int IVsSolutionEvents.OnBeforeUnloadProject(IVsHierarchy pRealHierarchy, IVsHierarchy pStubHierarchy) {
            if (ProjectClosed != null) {
                ProjectClosed(this, new EventArgs());
            }
            return VSConstants.S_OK;
        }
        
        /// <summary>
        /// Called when [before close solution].
        /// </summary>
        /// <param name="pUnkReserved">The p unk reserved.</param>
        /// <returns></returns>
        int IVsSolutionEvents.OnBeforeCloseSolution(object pUnkReserved) {
            return VSConstants.S_OK;
        }

        /// <summary>
        /// Called when [query close project].
        /// </summary>
        /// <param name="pHierarchy">The p hierarchy.</param>
        /// <param name="fRemoving">The f removing.</param>
        /// <param name="pfCancel">The pf cancel.</param>
        /// <returns></returns>
        int IVsSolutionEvents.OnQueryCloseProject(IVsHierarchy pHierarchy, int fRemoving, ref int pfCancel) {
            return VSConstants.S_OK;
        }

        /// <summary>
        /// Called when [query close solution].
        /// </summary>
        /// <param name="pUnkReserved">The p unk reserved.</param>
        /// <param name="pfCancel">The pf cancel.</param>
        /// <returns></returns>
        int IVsSolutionEvents.OnQueryCloseSolution(object pUnkReserved, ref int pfCancel) {
            return VSConstants.S_OK;
        }

        /// <summary>
        /// Called when [query unload project].
        /// </summary>
        /// <param name="pRealHierarchy">The p real hierarchy.</param>
        /// <param name="pfCancel">The pf cancel.</param>
        /// <returns></returns>
        int IVsSolutionEvents.OnQueryUnloadProject(IVsHierarchy pRealHierarchy, ref int pfCancel) {
            return VSConstants.S_OK;
        }
    }
}
